﻿// GoToLocation.js
/// <reference assembly="System.Web.Extensions" name="MicrosoftAjax.js"/>

Type.registerNamespace('ESRI.ADF.UI');

// base object for gotolocation
ESRI.ADF.UI.GoToLocation = function(element) {
    /// <summary>
    /// GoToLocation is a control for zooming and panning to particular co-ordinates on the map.
    /// </summary>
    ESRI.ADF.UI.GoToLocation.initializeBase(this, [element]);
    this._displayX = null;
    this._displayY = null;
    this._zoomToButton = null;
    this._panToButton = null;
    this._initialPositionSet = false;
}

ESRI.ADF.UI.GoToLocation.prototype = {
    initialize: function() {
        /// <summary>
        /// Initialize the parent along with any child controls that have not yet been initialized.
        /// </summary>
        ESRI.ADF.UI.GoToLocation.callBaseMethod(this, 'initialize');

        var id = this.get_id();
        var splitId = id.split('_');
        this._displayX = $get(splitId[0] + '_' + id + "_GoToLocationX");
        this._displayY = $get(splitId[0] + '_' + id + "_GoToLocationY");

        this._zoomToButton = $get(splitId[0] + '_' + id + "_ZoomTo");
        this._panToButton = $get(splitId[0] + '_' + id + "_PanTo");

        if (this._zoomToButton != null) {
            $addHandler(this._zoomToButton, "mousedown", Function.createDelegate(this, this._zoomTo));
            $addHandler(this._zoomToButton, "keydown", Function.createDelegate(this, this._zoomKeyPress));
        }
        if (this._panToButton != null) {
            $addHandler(this._panToButton, "mousedown", Function.createDelegate(this, this._panTo));
            $addHandler(this._panToButton, "keydown", Function.createDelegate(this, this._panKeyPress));
        }
        if (this.get_enableUpdates() == "True") {
            if (this._map != null)
                this._map.add_extentChanged(Function.createDelegate(this, this._updateCoords));
        }

        this._updateCoords();
    },

    get_map: function() {
        /// <value name="map" type="Object">Map control associated with the GoToLocation functionality</value>
        return this._map;
    },
    set_map: function(value) {
        this._map = value;
    },

    get_enableUpdates: function() {
        return this._enableUpdates;
    },
    set_enableUpdates: function(value) {
        this._enableUpdates = value;
    },

    get_zoomFactor: function() {
        return this._zoomFactor;
    },
    set_zoomFactor: function(value) {
        this._zoomFactor = value;
    },

    get_noOfDecimals: function() {
        return this._noOfDecimals;
    },
    set_noOfDecimals: function(value) {
        this._noOfDecimals = value;
    },

    pan: function(zoom) {
        /// <summary>
        /// Pans to location, with option to zoom in.
        /// </summary>
        /// <param name="zoom" type="Boolean">Optional. If true, function will zoom in. Defaults to false./param>
        var m = this.get_map();
        var x = Number.parseLocale(this._displayX.value);
        var y = Number.parseLocale(this._displayY.value);
        if (isNaN(x) || isNaN(y)) {
            alert("Cả hai giá trị phải là số.");
        }
        else {
            var point = new ESRI.ADF.Geometries.Point(parseFloat(x), parseFloat(y));
            if (zoom) {
                var width = m.get_extent().get_width() * parseFloat(this.get_zoomFactor());
                var height = m.get_extent().get_height() * parseFloat(this.get_zoomFactor());
                var box = new ESRI.ADF.Geometries.Envelope(point.get_x() - (width / 2), point.get_y() - (height / 2), point.get_x() + (width / 2), point.get_y() + (height / 2));
                m.zoomToBox(box);
            } else
                m.panTo(point);
        }
    },

    _updateCoords: function() {
        /// <summary>
        /// Updates the co-ordinate values 
        /// </summary>
        var center = this.get_map().get_center();
        var res = this.get_map().get_pixelSize();
        // get center coords of map to put into the xy textboxes
        var round = this.get_noOfDecimals();
        var dispX = this._round(center.get_x());
        var dispY = this._round(center.get_y());
        this._displayX.value = dispX.localeFormat("N" + round);
        this._displayY.value = dispY.localeFormat("N" + round);
    },
    _zoomTo: function() {
        /// <summary>
        /// Zoom to the co-ordinate values 
        /// </summary>
        this.pan(true);
    },
    _panTo: function() {
        /// <summary>
        /// Pan to the co-ordinate values 
        /// </summary>
        this.pan();
    },
    _zoomKeyPress: function(e) {
        if (e && e.keyCode == Sys.UI.Key.enter) {
            this.pan(true);
        }
    },
    _panKeyPress: function(e) {
        if (e && e.keyCode == Sys.UI.Key.enter) {
            this.pan();
        }
    },
    _roundToSignificant: function(value, resolution) {
        var round = Math.floor(-Math.log(resolution));
        if (round > 0) {
            round = Math.pow(10, round);
            return Math.round(value * round) / round;
        }
        else { return Math.round(value); }
    },
    _round: function(value) {
        var round = this.get_noOfDecimals();
        if (round > 0) {
            round = Math.pow(10, round);
            return Math.round(value * round) / round;
        }
        else { return Math.round(value); }
    }
}

ESRI.ADF.UI.GoToLocation.registerClass('ESRI.ADF.UI.GoToLocation', Sys.UI.Control);

if (typeof (Sys) !== "undefined") { Sys.Application.notifyScriptLoaded(); }


